/***************************************************************************
 $ @Description: 
 $ @Author: NavyPeng
 $ @Date: 2024-04-11 14:40:41
 $ @LastEditTime: 2024-04-19 15:12:41
 $ @LastEditors: NavyPeng
 ***************************************************************************/
#include "Ctrl.h"


#if MementoPattern_


class Memento
{
private:
	string state;
public:
	Memento() { state = ""; }

	Memento(string _state) {
		state = _state;
	}
	string GetState() const{
		return state;
	}
};



class Originator
{
private:
	string state;
public:
	void SetState(const string _state) {
		state = _state;
	}
	string GetState()const {
		return state;
	}
	Memento* CreateMemento() {
		return new Memento(state);
	}
	void SetMemento(Memento* memento) {
		state = memento->GetState();
	}
	void Show()const {
		cout << "State = " << state << endl;
	}
};


class Caretaker
{
private:
	Memento* memento;
public:

	void SetMemento(Memento* _memento) {
		memento = _memento;
	}
	Memento* GetMemento()const {
		return memento;
	}
};




class RoleStateMemento {
private:
	int vit;
	int atk;
	int def;
public:
	RoleStateMemento(int _vit, int _atk, int _def) {
		vit = _vit;
		atk = _atk;
		def = _def;
	}
	void SetVitality(const int _vit) {
		vit = _vit;
	}
	int GetVitality()const {
		return vit;
	}
	void SetAttack(const int _atk) {
		atk = _atk;
	}
	int GetAttack()const {
		return atk;
	}
	void SetDefense(const int _def) {
		def = _def;
	}
	int GetDefense()const {
		return def;
	}
};


class RoleStateCaretaker
{
private:
	RoleStateMemento* memento;
public:
	RoleStateMemento* GetMemento() const {
		return memento;
	}
	void SetMemento(RoleStateMemento* _memento) {
		memento = _memento;
	}
};



class GameRole
{
private:
	int vit;
	int atk;
	int def;
public:
	RoleStateMemento* SaveState() {
		return new RoleStateMemento(vit, atk, def);
	}
	void SetVitality(const int _vit) {
		vit = _vit;
	}
	int GetVitality()const {
		return vit;
	}
	void SetAttack(const int _atk) {
		atk = _atk;
	}
	int GetAttack()const {
		return atk;
	}
	void SetDefense(const int _def) {
		def = _def;
	}
	int GetDefense()const {
		return def;
	}
	void StateDisplay()const {
		cout << "角色当前状态：" << endl;
		cout << "体力：" << vit << endl;
		cout << "攻击力：" << atk << endl;
		cout << "防御力：" << def << endl;
		cout << endl;
	}
	void GetInitState() {
		vit = 100;
		atk = 100;
		def = 100;
	}
	void Fight() {
		vit = 0;
		atk = 0;
		def = 0;
	}
	void RecoveryState(RoleStateMemento* _memento) {
		vit = _memento->GetVitality();
		atk = _memento->GetAttack();
		def = _memento->GetDefense();
	}

};





int main() {
	// 示例1
	Originator* pO = new Originator();
	pO->SetState("On");
	pO->Show();

	Caretaker* pC = new Caretaker();
	pC->SetMemento(pO->CreateMemento());

	pO->SetState("Off");
	pO->Show();

	pO->SetMemento(pC->GetMemento());
	pO->Show();

	
	
	
	// 示例2
	

	// 大战BOSS前
	GameRole* lixiaoyao = new GameRole();
	lixiaoyao->GetInitState();
	lixiaoyao->StateDisplay();
	
	// 保存进度
	RoleStateCaretaker* stateAdmin = new RoleStateCaretaker();
	stateAdmin->SetMemento(lixiaoyao->SaveState());

	// 大战BOSS，损耗严重
	lixiaoyao->Fight();
	lixiaoyao->StateDisplay();

	// 恢复之前状态
	lixiaoyao->RecoveryState(stateAdmin->GetMemento());
	lixiaoyao->StateDisplay();

}





#endif 